// example/login/login.js
var app = getApp()
import * as Api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColors: '',

    hasGoneAway: false,
    loginName: "",
    password: "",
    main_icon: ''
  },
  goForget() {
    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })

      wx.redirectTo({
        url: '../forgetpassword/forgetpassword',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  doLogin() {


    if (!this.data.loginName.length > 0 && !this.data.password.length > 0) {
      return
    }


    if (this.data.loginName.length!=11) {
      wx.showModal({
        title: '',
        content: '手机号格式错误',
        showCancel: false,
        cancelText: '',
        cancelColor: '',
        confirmText: '知道了',
        confirmColor: '',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
      return;
    } else {



      let self = this
      let url = app.globalData.baseUrl1 + '/auth/user/login';
      let data = {
        loginName: this.data.loginName,
        password: this.data.password,
        openId: app.globalData.openid,
        merchantId: app.globalData.merchant.merchantId
      }
      let callBack = (res) => {

        if (res == null) {

          console.log("网络请求错误！");
          return;
        }

        if (res.data.status != 200) {

          if (res.data.message === '用户审核未通过') {
          wx.showModal({
            content: '账号已冻结，请联系管理员',
            showCancel: false,
            confirmText: '知道了',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
          return
        }

          wx.showModal({
            title: '',
            content: res.data.message,
            showCancel: false,
            cancelText: '',
            cancelColor: '',
            confirmText: '知道了',
            confirmColor: '',
            success: function (res) {

            },
            fail: function (res) { },
            complete: function (res) { },
          })
          return;
        }
        if (res.data.status === 200) {
          if (res.data.data.userStatus === 'USER_CHECK_SUCCEED') {

       
            wx.showToast({
              title: '登录成功',
              icon: 'siuccess',
              duration: 2000,
              mask: true,
              success: function (res1) {

                if (!self.data.hasGoneAway) {
                  self.setData({ hasGoneAway: true })
                  console.log('登录成功', res.data.data.token)
                  
                  app.globalData.userId = res.data.data.userId
                  app.globalData.token = res.data.data.token
                  console.log(app.globalData.token)
                wx.navigateBack({
                  delta: 1
                })
                }
              },
              fail: function (res) { },
              complete: function (res) { },
            })

          } else if (res.data.data.userStatus === 'USER_UNCHECK'){
            wx.showModal({
              content: '账号等待审核中',
              showCancel: false,
              confirmText: '知道了',
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          }
        }
      }
      Api.reqFormPost(url, data, callBack)
    }
  },
  changeInputLoginName(e) {
    this.setData({
      loginName: e.detail.value
    })
  },
  changeInputPassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  goSign() {

    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })
      wx.redirectTo({
        url: '../sign/sign',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }

    this.setData({
      main_icon: app.globalData.merchant.merchantIcon,
      themeColors: app.globalData.merchant.themeColors
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.merchant === null) {
      return
    } 
    this.setData({ hasGoneAway: false })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})